package ca.etsmtl.log430.lab3;

/**
 * This class presents the user with menus, accepts their choice, ensures their
 * choice is valid, and returns their choice to the caller.
 * 
 * The menu offers the following choices:
 * 
 * <pre>
 *    1) List Students
 *    2) List Courses
 *    3) List Courses a Student has Registered for
 *    4) List Students Registered for a Course
 *    5) Register a Student for a Course
 *    X) Exit
 * </pre>
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */

/*
 * Modification Log **********************************************************
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */

public class Menus {

	/**
	 * Main menu.
	 * 
	 * @return the user's choice in the menu.
	 */
	public char mainMenu() {
		Termio terminal = new Termio();
		char userChoice = ' ';
		boolean error = true;

		while (error) {
			System.out.println("\n\n1) List Students");
			System.out.println("2) List Courses");
			System.out.println("3) List Courses a Student has Registered for");
			System.out.println("4) List Students Registered for a Course");
			System.out.println("5) Register a Student for a Course");
			System.out.println("X) Exit");
			System.out.print("\nEnter your choice and press return >> ");

			userChoice = terminal.keyboardReadChar();

			if ((userChoice != 'X') && (userChoice != 'x')
					&& (userChoice < '1') && (userChoice != '2')
					&& (userChoice != '3') && (userChoice < '4')
					&& (userChoice != '5')) {
				System.out.print("\n\n*** Invalid Choice:: " + userChoice
						+ " ***");
			} else {
				error = false;
			}
		}
		return (userChoice);
	}

	/**
	 * Allows the user to select a student.
	 * 
	 * @param listObject
	 * @return the Student the user has chosen.
	 */
	public Student pickStudent(StudentList listObject) {
		Termio terminal = new Termio();
		String userChoice;
		Student studentObject = null;

		System.out.print("\n\nEnter Student ID and press return >> ");
		userChoice = terminal.keyboardReadString();

		studentObject = listObject.findStudentByID(userChoice);

		if (studentObject == null) {
			System.out.println("\n\n*** Student ID " + userChoice
					+ " not found ***");
		}
		return (studentObject);
	}

	/**
	 * Allows the user to select a student.
	 * 
	 * @param listObject
	 * @return
	 */
	public Course pickCourse(CourseList listObject) {
		Termio terminal = new Termio();
		String userChoiceCourseID;
		String userChoiceSection;
		Course courseObject = null;

		System.out.print("\n\nEnter Course ID and press return >> ");
		userChoiceCourseID = terminal.keyboardReadString();

		System.out.print("\n\nEnter Course Section and press return >> ");
		userChoiceSection = terminal.keyboardReadString();

		courseObject = listObject.findCourseByIDAndSection(userChoiceCourseID,
				userChoiceSection);

		if (courseObject == null) {
			System.out.print("\n\n*** Course ID:" + userChoiceCourseID);
			System.out.println(" Section:" + userChoiceSection
					+ " not found ***");
		}
		return (courseObject);
	}
}